<?php 
class EmailTemplate {
	const MAX_MESSAGE_BODY_LENGTH = 50;
	
	const TAG_RECIPIENT_NAME = "#RECIPIENT_NAME";
	const TAG_SENDER_NAME = "#SENDER_NAME";
	const TAG_URL = "#URL";
	const TAG_MESSAGE_BODY = "#MESSAGE_BODY";
	const TAG_BOOK_NAME = "#BOOK_NAME";
	
	const TEMPLATE_USER_MESSAGE = "Hello, #RECIPIENT_NAME, <br/>#SENDER_NAME sent you a new message, <br/><br/>\"#MESSAGE_BODY\"<br/><br/>Click the following link to see the detail<br/><br/><a href=\"#URL\" target=\"_blank\">#URL</a><br/><br/>";
	const TEMPLATE_USER_MESSAGE_WITH_BOOK = "Hello, #RECIPIENT_NAME, <br/>#SENDER_NAME sent you a new message, about your book <strong>#BOOK_NAME</strong> <br/><br/>\"#MESSAGE_BODY\"<br/><br/>Click the following link to see the detail<br/><br/><a href=\"#URL\" target=\"_blank\">#URL</a><br/><br/>";
	const TEMPLATE_RESET_PASSWORD = "Hello, #RECIPIENT_NAME, <br/>You have required to reset your password, click the link below to proceed reseting your password.<br/><br/>#URL";
	
	const SUBJECT_USER_MESSAGE = "You got a new message";
	const SUBJECT_USER_ACTIVATE = "Welcome to HeyIdleBooks! Please activate your account.";
	const SUBJECT_RESET_PASSWORD = "Reset password";
	
	/**
	 * Return user activate notfication message.
	 * 
	 * @param $user
	 * @param $registrationKey
	 */
	public static function getUserActivateMessage($email, $registrationKey) {
		$config = Zend_Registry::get('config');
		$message = "<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />";
		$message .= "Welcome to HeyIdleBooks! To activate your account, please click on the following link:<br />";
		$message .= $config->baseurl."/user/activate?key=".$registrationKey."&email=".$email."<br />";
		$message .= "If clicking this link does not work, copy and paste it into your web browser.<br />";
		$message .= "If you have any questions about HeyIdleBooks, you might contact with us at http://heyidlebooks.com/contact<br />";
		$message .= "The HeyIdleBooks Team";
		return $message;
	}
	
	/**
	 * Get email message.
	 * 
	 * @param $type
	 * @param $senderUser
	 * @param $targetUser
	 * @param $extra extra info
	 */
	public static function getEmailMessage($type, $senderUser, $targetUser, $extra) {
		$emailMessage = "";
		$config = Zend_Registry::get("config");
		switch ($type) {
			case Activity::SEND_USER_MESSAGE:
				$book = $extra["book"];
				if (isset($book)) {
					$emailMessage = str_replace(self::TAG_RECIPIENT_NAME, $targetUser->name, self::TEMPLATE_USER_MESSAGE_WITH_BOOK);
				} else {
					$emailMessage = str_replace(self::TAG_RECIPIENT_NAME, $targetUser->name, self::TEMPLATE_USER_MESSAGE);
				}
				$emailMessage = str_replace(self::TAG_BOOK_NAME, $book->name, $emailMessage);
				$threadId = $extra["thread"];
				$url = $config->baseurl."message/view/tid/".$threadId;
				$emailMessage = str_replace(self::TAG_SENDER_NAME, $senderUser->name, $emailMessage);
				$emailMessage = str_replace(self::TAG_URL, $url, $emailMessage);
				
				// limit max message text in the email
				$messageLimited = $extra["message"];
				if (strlen($messageLimited) > self::MAX_MESSAGE_BODY_LENGTH) {
					$messageLimited = substr($messageLimited, 0, self::MAX_MESSAGE_BODY_LENGTH)."...";
				}
				$emailMessage = str_replace(self::TAG_MESSAGE_BODY, $messageLimited, $emailMessage);
				
				break;
			case Activity::RESET_PASSWORD:
				$url = $config->baseurl."/user/resetpassword/key/".$extra["key"];
				$emailMessage = str_replace(self::TAG_RECIPIENT_NAME, $targetUser->email, self::TEMPLATE_RESET_PASSWORD);
				$emailMessage = str_replace(self::TAG_URL, $url, $emailMessage);
				break;
		}
		return $emailMessage;
	}
}
?>